package com.neolix.watch.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.neolix.watch.R;

import static com.neolix.watch.NeolixApplication.getInstance;

public class ErrorPopWindowsUtil {

    public static PopupWindow createPopWindowAndShow(String errorText, View parentView) {
        return createPopWindowAndShow(errorText, parentView, false);
    }

    public static PopupWindow createPopWindowAndShow(String errorText, View parentView, boolean isBelow) {
        PopupWindow popupWindow = new PopupWindow(getInstance());
        View summaryContent = initSummaryContentView(errorText);
        popupWindow.setContentView(summaryContent);

        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getInstance().getResources().getDrawable(R.color.transparent));

        popupWindow.setFocusable(false);
        popupWindow.setOutsideTouchable(false);

        setTag(parentView, popupWindow);

        if (isBelow)
            popupWindow.showAsDropDown(parentView);
        else
            popupWindow.showAsDropDown(parentView, getLoginPanelMarginLeft(parentView)[0], -getLoginPanelMarginLeft(parentView)[1]);

        return popupWindow;
    }

    public static PopupWindow createPopWindowAndShowAt(String errorText, View parentView, int x, int y, int offsetY) {
        PopupWindow popupWindow = new PopupWindow(getInstance());
        View summaryContent = initSummaryContentView(errorText);
        popupWindow.setContentView(summaryContent);

        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getInstance().getResources().getDrawable(R.color.transparent));

        popupWindow.setFocusable(false);
        popupWindow.setOutsideTouchable(false);

        popupWindow.setAnimationStyle(R.style.popwindow_anim_style);
        popupWindow.showAtLocation(parentView, Gravity.CENTER, x, y - popupWindow.getWidth() + offsetY);

        return popupWindow;
    }

    public static PopupWindow createPointPopWindowAndShowAt(int backgroundResourceId, View parentView, int x, int y) {
        PopupWindow popupWindow = new PopupWindow(getInstance());
        View summaryContent = initImageView(backgroundResourceId);
        popupWindow.setContentView(summaryContent);

        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(getInstance().getResources().getDrawable(R.color.transparent));

        popupWindow.setFocusable(false);
        popupWindow.setOutsideTouchable(false);

        setTag(parentView, popupWindow);

        popupWindow.setAnimationStyle(R.style.popwindow_anim_style);
        popupWindow.showAtLocation(parentView, Gravity.CENTER, x, y);

        return popupWindow;
    }

    public static PopupWindow getPopupWindow(View parentView) {
        Object parentViewTag = parentView.getTag(R.id.popup_window_tag);
        try {
            return (PopupWindow) parentViewTag;
        } catch (Exception ex) {
            return null;
        }
    }

    public static void closePopupWindow(View view) {
        PopupWindow popupWindow = getPopupWindow(view);
        if (popupWindow == null)
            return;
        popupWindow.dismiss();
    }

    private static void setTag(View parentView, PopupWindow popupWindow) {
        PopupWindow window = getPopupWindow(parentView);
        if (window != null)
            window.dismiss();
        parentView.setTag(R.id.popup_window_tag, popupWindow);
    }

    private static int[] getLoginPanelMarginLeft(View view) {
        int location[] = new int[2];
        location[0] = view.getWidth() / 3 * 2;
        location[1] = view.getHeight() / 2 + 20;
        return location;
    }

    private static View initSummaryContentView(String errorText) {
        LayoutInflater inflater = (LayoutInflater) getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View summaryContent = inflater.inflate(R.layout.view_pop_window, null);
        TextView summaryTextView = (TextView) summaryContent.findViewById(R.id.error_text);
        summaryTextView.setText(errorText);
        return summaryContent;
    }

    private static View initImageView(int backgroundResourceId) {
        ImageView view = new ImageView(getInstance());
        view.setImageResource(backgroundResourceId);
//        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return view;
    }
}
